﻿using System;
using System.Collections.Generic;
using System.IO;

namespace UpdaterForm.CustomTreeView
{
    public class FolderNode : ATreeNode
    {
        private readonly string _targetDir;
        private DirectoryInfo _directory;

        public FolderNode(DirectoryInfo directory, String targetDir)
        {
            _targetDir = targetDir;
            _directory = directory;
            Name = directory.Name;
            Children = new List<ATreeNode>();
            var subDirs = _directory.GetDirectories();
            foreach (var subDir in subDirs)
            {
                var subTargetDir = Path.Combine(_targetDir, subDir.Name);
                Children.Add(new FolderNode(subDir, subTargetDir));
            }

            var files = _directory.GetFiles();
            foreach (FileInfo file in files)
            {
                var node = new FileNode(file, _targetDir);
                Children.Add(node);
            }
        }


        public override void Initialize()
        {
            SetParent();
        }

        public override void Apply(bool custom)
        {
            if (!Directory.Exists(_targetDir))
                Directory.CreateDirectory(_targetDir);

            foreach (ATreeNode child in Children)
                child.Apply(custom);
        }
    }
}